package com.ruoyi.framework.mybatisplus.handle;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.insert.Insert;
import org.springframework.stereotype.Component;

/**
 * @author yql
 * @describe GenerateUuidHandler
 * @date 2023/3/13 17:07
 */
//@Component
public class UserIdGenerateHandler implements GenerateHandler {


    @Override
    public Expression getValue() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return new LongValue(user.getUserId());
    }

    @Override
    public String getFieldName() {
        return "user_id";
    }

    @Override
    public boolean off(Statement statement) {
        try {
            SecurityUtils.getLoginUser();
            return statement instanceof Insert;
        } catch (Exception e) {
            return false;
        }
    }
}
